package com.main;

import java.util.*;

import Neighborhood.NearestNUserNeighborhood;
import Neighborhood.Neighborhood;

import com.searchingmodule.SearchingElement;
import com.util.*;
import com.imagefinder.*;
import com.recommender.*;
import com.model.*;

public class Factory {

	private ImageFinder imagefinder = null;
	private DataModel datamodel = null;
	private Recommender recommender = null;
	private SearchingElement element = new SearchingElement();
	private Neighborhood neighborhood = new NearestNUserNeighborhood(25);

	protected String datasource = "tagbasedrecommender";
	private String url = "jdbc:mysql://localhost/";
	private String username = "root";
	private String password = "";
	private String driverName = "com.mysql.jdbc.Driver";

	public SearchingElement getSearchingElement(String apiKey, String passWd)
			throws Exception {
		XMLReader xmlReader = new XMLReader();
		String searchEngine = xmlReader.getSearchEngine(apiKey);
		System.out.println(searchEngine);
		String algorithm = xmlReader.getServiceAlgorithm(apiKey).trim();

		password = xmlReader.getPassWord(apiKey);

		datasource = xmlReader.getDatabaseName(apiKey);
		if (searchEngine.equalsIgnoreCase("Google"))
			imagefinder = new GoogleImageFinder(); // code added to use search
													// engines
		else if (searchEngine.equalsIgnoreCase("Yahoo"))
			imagefinder = new YahooImageFinder();

		if (algorithm.equalsIgnoreCase("0")) {
			ArrayList<String> contextvariables = xmlReader
					.getContextVariables(apiKey);
			System.out.println("Inre");
			recommender = new SimpleRecommender();
			datamodel = new StructuredContextDataModel(datasource, url,
					username, password, driverName);
			datamodel.setContextVariables(contextvariables);

		} else if (algorithm.equalsIgnoreCase("2")) {

			recommender = new TagBasedRecommender();
			datamodel = new TagBasedDataModel(datasource, url, username,
					password, driverName);
		} else if (algorithm
				.equalsIgnoreCase("3")) {
			recommender = new UserAwareTagBasedRecommender();
			datamodel = new TagBasedUserDataModel(datasource, url, username,
					password, driverName);
		} else if (algorithm.equalsIgnoreCase("1")) {
			recommender = new UserBasedRecommender();
			datamodel = new StructuredContextUserBasedModel(datasource, url,
					username, password, driverName);
			ArrayList<String> contextvariables = xmlReader
					.getContextVariables(apiKey);
			datamodel.setContextVariables(contextvariables);
		}else if(algorithm.equalsIgnoreCase("4"))
		{
			recommender = new ProbabilityBaseRecommender();
			datamodel = new ProbabilisticDataModel(datasource, url,
					username, password, driverName);
			ArrayList<String> contextvariables = xmlReader
					.getContextVariables(apiKey);
			datamodel.setContextVariables(contextvariables);
			
		}

		element.setRecommender(recommender);
		element.setDataModel(datamodel);
		element.setImageFinder(imagefinder);
		element.setNeighborhood(neighborhood);
		return element;

	}

}
